/**
 * $Id: GatewayInfo.java,v 1.16 2006/09/08 08:16:18 martin Exp $
 *
 * Copyright 2006 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.connector.Connector;
import com.haoxi.gateway.messageconvertor.MessageConvertor;

/**
 * TODO
 * 
 * Revision History
 * 
 * 2006-6-12,star.Zhao,created it
 */
public class GatewayInfo {
    private static Log log = LogFactory.getLog(GatewayInfo.class);
    /**
     * 对应网关编号
     */
    private String gatewayCode;

    /**
     * 网关对应的运营商
     */
    private String opName;

    /**
     * 短信接入号
     */
    private String spNumber;

    private String unicom_nodeID;
    private static final Object syn_mo_container_obj = new Object();
    private static final Object syn_mt_container_obj = new Object();
    /**
     * 网关对应的运营商编号
     */
    private int opCode;

    private String icp_id;
    private String loginName;
    private String password;

    /**
     * 网关用的协议名
     */
    private String protocolName;
    /**
     * 网关的协议版本号
     */
    private String protocolVersion; 
    /**
     * activeTest 心跳内容
     */
    private String activeTestContent;

    /**
     * 网关对应的消息转换器
     */
    private MessageConvertor convertor;

    /**
     * 可以下发MT的connectors
     */
    private Map canReciveDeliverConns = new HashMap();

    /**
     * 不可以下发MT的connectors
     */
    private Map canSubmitMTConns = new HashMap();

    /**
     * 记录可以允许连接服务端， UnicomSgipDeliver
     */
    private List serverConnectors = new ArrayList();

    /**
     * 记录配置的client端， UnicomSgipDeliver
     */
    private List clilentConnectors = new ArrayList();

    /**
     * @param connector
     */
    public void addServerConnector(Connector connector) {
        if (null == connector) {
            throw new IllegalArgumentException("Inviad argument!");
        }

        if (null == serverConnectors) {
            serverConnectors = new ArrayList();
        }
        serverConnectors.add(connector);
    }

    /**
     * 获得所有ServerConnector
     * 
     * @return
     */
    public List getServerConnectors() {
        if (null == serverConnectors) {
            serverConnectors = new ArrayList();
        }
        return serverConnectors;
    }

    /**
     * 获得所有可以连接
     * 
     * @return
     */
    public List getAllConnectors() {
        List result = new ArrayList();

        // 可下发的连接
        Iterator keyIt = canReciveDeliverConns.keySet().iterator();
        while (keyIt.hasNext()) {
            result.add(canReciveDeliverConns.get(keyIt.next()));
        }

        // 不可下发的连接
        Iterator keyItr = canSubmitMTConns.keySet().iterator();
        while (keyItr.hasNext()) {
            Object object = keyItr.next();
            if (!canReciveDeliverConns.containsKey(object)) {
                result.add(canSubmitMTConns.get(object));
            }
        }

        return result;
    }

    /**
     * 获得所有可以连接
     * 
     * @return
     */
    public List getCanReciveDeliverConnectors() {
        List result = new ArrayList();

        // 不可下发的连接
        Iterator keyItr = canReciveDeliverConns.keySet().iterator();
        while (keyItr.hasNext()) {
            result.add(canReciveDeliverConns.get(keyItr.next()));
        }

        return result;
    }

    /**
     * 获得所有可以下发MT连接
     * 
     * @return
     */
    public List getCanSubmitMTConnectors() {
        List result = new ArrayList();

        // 可下发的连接
        Iterator keyIt = canSubmitMTConns.keySet().iterator();
        while (keyIt.hasNext()) {
            result.add(canSubmitMTConns.get(keyIt.next()));
        }

        return result;
    }

    /**
     * @param connectorName
     * @param connector
     */
    public boolean addClientConnector(Connector connector) {
        if (null == connector) {
            log.error("construct Connector fail, please check it!");
            return false;
        }
        synchronized (clilentConnectors) {
            clilentConnectors.add(connector);
        }
        return true;
    }

    /**
     * @param connectorName
     * @param connector
     */
    public List getClientConnectors() {
        if (null == clilentConnectors) {
            clilentConnectors = new ArrayList();
        }
        return clilentConnectors;
    }
    
    /**
     * @param connectorName
     * @param connector
     */
    public boolean addCanSubmitMTConn(String connectorName, Connector connector) {
        if (canSubmitMTConns.containsKey(connectorName)) {
            log
                    .error("The gateway cannot configs the same name connector! PLease check it!");
            return false;
        }
        synchronized (syn_mt_container_obj) {
            canSubmitMTConns.put(connectorName, connector);
        }

        return true;
    }

    /**
     * @param connectorName
     * @param connector
     */
    public boolean addCanReciveDeliverConn(String connectorName,
            Connector connector) {
        if (canReciveDeliverConns.containsKey(connectorName)) {
            log
                    .error("The gateway cannot configs the same name connector! PLease check it!");
            return false;
        }
        synchronized (syn_mo_container_obj) {
            canReciveDeliverConns.put(connectorName, connector);
        }
        return true;
    }

    /**
     * @param connectorName
     */
    public void removeCanSubmitMTConn(String connectorName) {
        synchronized (syn_mt_container_obj) {
            canSubmitMTConns.remove(connectorName);
        }
    }

    public void removeCanReciveDeliverConn(String connectorName) {
        synchronized (syn_mo_container_obj) {
            canReciveDeliverConns.remove(connectorName);
        }
    }

    /**
     * @return Returns the convertor.
     */
    public MessageConvertor getConvertor() {
        return convertor;
    }

    /**
     * @param convertor The convertor to set.
     */
    public void setConvertor(MessageConvertor convertor) {
        this.convertor = convertor;
    }

    /**
     * @return Returns the gatewayCode.
     */
    public String getGatewayCode() {
        return gatewayCode;
    }

    /**
     * @param gatewayCode The gatewayCode to set.
     */
    public void setGatewayCode(String gatewayCode) {
        this.gatewayCode = gatewayCode;
    }

    /**
     * @return Returns the opCode.
     */
    public int getOpCode() {
        return opCode;
    }

    /**
     * @param opCode The opCode to set.
     */
    public void setOpCode(int opCode) {
        this.opCode = opCode;
    }

    /**
     * @return Returns the opName.
     */
    public String getOpName() {
        return opName;
    }

    /**
     * @param opName The opName to set.
     */
    public void setOpName(String opName) {
        this.opName = opName;
    }

    /**
     * @return Returns the protocolName.
     */
    public String getProtocolName() {
        return protocolName;
    }

    /**
     * @param protocolName The protocolName to set.
     */
    public void setProtocolName(String protocolName) {
        this.protocolName = protocolName;
    }
    /**
     * @return Returns the protocolVersion.
     */
    public String getProtocolVersion() {
        return protocolVersion;
    }

    /**
     * @param protocolVersion The protocolVersion to set.
     */
    public void setProtocolVersion(String protocolVersion) {
        this.protocolVersion = protocolVersion;
    }

    /**
     * @return Returns the icp_id.
     */
    public String getIcp_id() {
        return icp_id;
    }

    /**
     * @param icp_id The icp_id to set.
     */
    public void setIcp_id(String icp_id) {
        this.icp_id = icp_id;
    }

    /**
     * @return Returns the password.
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password The password to set.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return Returns the canReciveDeliverConns.
     */
    public Map getCanReciveDeliverConns() {
        return canReciveDeliverConns;
    }

    /**
     * @return Returns the canSubmitMTConns.
     */
    public Map getCanSubmitMTConns() {
        return canSubmitMTConns;
    }

    /**
     * @return Returns the activeTestContent.
     */
    public String getActiveTestContent() {
        return activeTestContent;
    }

    /**
     * @param activeTestContent The activeTestContent to set.
     */
    public void setActiveTestContent(String activeTestContent) {
        this.activeTestContent = activeTestContent;
    }

    /**
     * @return Returns the spNumber.
     */
    public String getSpNumber() {
        return spNumber;
    }

    /**
     * @param spNumber The spNumber to set.
     */
    public void setSpNumber(String spNumber) {
        this.spNumber = spNumber;
    }

    /**
     * @return Returns the loginName.
     */
    public String getLoginName() {
        return loginName;
    }

    /**
     * @param loginName The loginName to set.
     */
    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    /**
     * @return Returns the unicom_nodeID.
     */
    public String getUnicom_nodeID() {
        return unicom_nodeID;
    }

    /**
     * @param unicom_nodeID The unicom_nodeID to set.
     */
    public void setUnicom_nodeID(String unicom_nodeID) {
        this.unicom_nodeID = unicom_nodeID;
    }

}
